const router = require('koa-router')()
const { Fb } = require('../class/utils')
const Address = require('../class/address')

router.prefix('/')

// 添加地区
router.post('/createAddress.do', async ctx => {
  const form = ctx.request.body
  if (form.type == 1) {
    ctx.body = await Fb.callback(Address.createCity(form))
  } else {
    ctx.body = await Fb.callback(Address.create(form))
  }
})

// 城市和地区列表
router.post('/addressList.do', async ctx => {
  const form = ctx.request.body
  let nowPage = parseInt(form.nowPage) || 1
  let pageSize = parseInt(form.pageSize) || 9999999
  ctx.body = await Fb.callback(Address.list(form, nowPage, pageSize))
})

// 删除地址
router.post('/delAddress.do', async ctx => {
  const form = ctx.request.body
  if (form.type == 1) {
    ctx.body = await Fb.callback(Address.delCity(form))
  } else {
    ctx.body = await Fb.callback(Address.delArea(form))
  }
})

// 获取城市列表
router.post('/getCityList.do', async ctx => {
  ctx.body = await Fb.callback(Address.getCityList())
})

// 获取地区列表
router.post('/getAreaList.do', async ctx => {
  const form = ctx.request.body
  if (form.type == 1) {
      console.log('1');
    ctx.body = await Fb.callback(Address.getAllAreaList(form))
  } else {
    ctx.body = await Fb.callback(Address.getAreaList(form))
  }
})

module.exports = router
